package editeurGraphiti.features;

import modeleProcessus.Activite;

import org.eclipse.emf.ecore.EReference;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddConnectionContext;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.impl.AbstractAddFeature;
import org.eclipse.graphiti.mm.GraphicsAlgorithmContainer;
import org.eclipse.graphiti.mm.algorithms.Polyline;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.IGaService;
import org.eclipse.graphiti.services.IPeCreateService;
import org.eclipse.graphiti.util.ColorConstant;
import org.eclipse.graphiti.util.IColorConstant;

import editeurGraphiti.constants.Constants;

public class AddConnectionLienFeature extends AbstractAddFeature {
	

	public AddConnectionLienFeature(IFeatureProvider fp) {
		super(fp);
	}

	@Override
	public boolean canAdd(IAddContext context) {
		if (context instanceof IAddConnectionContext) {
			return true;
		}
		return false;
	}

	@Override
	public PictogramElement add(IAddContext context) {
        IAddConnectionContext addConContext = (IAddConnectionContext) context;
        EReference addedEReference = (EReference) context.getNewObject();
        IPeCreateService peCreateService = Graphiti.getPeCreateService();        
        // CONNECTION WITH POLYLINE
        Connection connection = peCreateService
            .createFreeFormConnection(getDiagram());
        connection.setStart(addConContext.getSourceAnchor());
        connection.setEnd(addConContext.getTargetAnchor());
        
        Shape sourceShape = (Shape) addConContext.getSourceAnchor().eContainer();
        Shape targetShape = (Shape) addConContext.getTargetAnchor().eContainer();

        Object sourceActivite = getBusinessObjectForPictogramElement(sourceShape);
        Object targetActivite = getBusinessObjectForPictogramElement(targetShape);
        
        
        IGaService gaService = Graphiti.getGaService();
        Polyline polyline = gaService.createPolyline(connection);
        polyline.setLineWidth(2);
        if (sourceActivite instanceof Activite && targetActivite instanceof Activite) {
            polyline.setForeground(manageColor(Constants.E_ACTIVITE_FOREGROUND));
            ConnectionDecorator cd;
            cd = peCreateService
                  .createConnectionDecorator(connection, false, 1.0, true);
            createArrow(cd);
        } else {
            polyline.setForeground(manageColor(Constants.E_REFERENCE_FOREGROUND));
            // create link and wire it
            //link(connection, addedEReference);
        }
        return connection;
	}

	
	private Polyline createArrow(GraphicsAlgorithmContainer gaContainer) {
	    IGaService gaService = Graphiti.getGaService();
	    Polyline polyline =
	        gaService.createPolyline(gaContainer, new int[] { -15, 10, 0, 0, -15,
	                -10 });
	    polyline.setForeground(manageColor(Constants.E_ACTIVITE_FOREGROUND));
	    polyline.setLineWidth(2);
	    return polyline;
	} 

}
